import glob
import os


class Rename01:
    """
    批量将类似【错过的烟火 – 周杰伦.mp3】的文件重命名为【周杰伦 – 错过的烟火.mp3】
    """

    def __init__(self, p_dir):
        """
        初始化

        :param p_dir: 指定文件夹
        """
        self.dir = p_dir

    def execute(self):
        t_files = sorted(os.listdir(self.dir))
        for t_file in t_files:
            fname = os.path.basename(t_file)
            parts = fname.split(".")
            fnme = parts[0]
            fext = parts[1]
            # 获取文件名中的艺术家名和歌曲名
            separator = ' – '
            parts = fnme.split(separator)
            artist = parts[1]
            song = parts[0]

            # 重命名文件
            new_filename = f"{artist} - {song}.{fext}"
            os.rename(os.path.join(self.dir, t_file), os.path.join(self.dir, new_filename))

    def execute2(self):
        pattern = "* – *.*"
        t_files = glob.glob(self.dir + '/' + pattern)
        for t_file in t_files:
            fname = os.path.basename(t_file)
            parts = fname.split(".")
            fnme = parts[0]
            fext = parts[1]
            # 获取文件名中的艺术家名和歌曲名
            separator = ' – '
            parts = fnme.split(separator)
            artist = parts[0]
            song = parts[1]
            # 重命名文件
            new_filename = f"{artist} - {song}.{fext}"
            os.rename(os.path.join(self.dir, t_file), os.path.join(self.dir, new_filename))
